----------------------------
-- Simplistic Street
-- by Arran
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------
require( "glon" )

if SERVER then
	
	local KILLS = {}
	KILLS.List = {}
	KILLS.File = "Utopia/killdata.txt"
	
	function KILLS:LoadKills()
		if not file.Exists(self.File) then file.Write(self.File,glon.encode({})) 
		else
			self.List = glon.decode( file.Read(self.File) )
			if not self.List then
				self.List = {}
				print( "Kills list didn't exist, using blank table" )
			end
		end
	end
	
	function KILLS:SaveKills()
		file.Write( self.File, glon.encode( self.List ) )
	end
	
	function KILLS:SendList( pl )
		for _,v in pairs( self.List ) do
			umsg.Start("StreamKills",pl)
				umsg.String( v.Time )
				umsg.String( v.Killer )
				umsg.String( v.Victim )
			umsg.End()
		end
	end
	
	hook.Add("PlayerAuthed","KILLSSendData",function(pl)
		if pl:IsAdmin() then KILLS:SendList( pl ) end
	end)
	
	hook.Add("ShutDown","KILLSSave",function() KILLS:SaveKills() end)
			
	
	function KILLS.RetreiveKillsDeaths(victim, inflictor, killer)
		
		if killer:IsPlayer() and victim:IsPlayer() then
		
		local rp = RecipientFilter()
		for _,v in pairs( player.GetAll() ) do if v:IsAdmin() then rp:AddPlayer(v) end end
		umsg.Start("StreamKills",rp)
			umsg.String( os.date("%d/%m/%y %I:%M%p") )
			umsg.String( killer:GetName() .." - ".. killer:SteamID() )
			umsg.String( victim:GetName() )
		umsg.End()
		table.insert( KILLS.List, { Time = os.date("%d/%m/%y %I:%M%p"), Killer = killer:GetName() .." - ".. killer:SteamID(), Victim = victim:GetName() } )
		
	end
end

	hook.Add("PlayerDeath", "BangHeadshot", KILLS.RetreiveKillsDeaths)
	
	KILLS:LoadKills()
	
	
	
	
--------------------Chats----------------------------
	
	
	local CHAT = {}
	CHAT.List = {}
	CHAT.File = "Utopia/chatdata.txt"
	
	function CHAT:LoadChats()
		if not file.Exists(self.File) then file.Write(self.File,glon.encode({})) 
		else
			self.List = glon.decode( file.Read(self.File) )
			if not self.List then
                		self.List = {}
                		print( "Chats list didn't exist, using blank table" )
            		end
		end
	end
	
	function CHAT:SaveChats()
		file.Write( self.File, glon.encode( self.List ) )
	end
	
	function CHAT:SendList( pl )
		for _,v in pairs( self.List ) do
			umsg.Start("StreamChats",pl)
				umsg.String( v.Time )
				umsg.String( v.Player )
				umsg.String( v.SteamID )
				umsg.String( v.Message )
			umsg.End()
		end
	end
	
	hook.Add("PlayerAuthed","CHATSendData",function(pl)
		if pl:IsAdmin() then CHAT:SendList( pl ) end
	end)
	
	hook.Add("ShutDown","CHATSave",function() CHAT:SaveChats() end)
			
	
	function CHAT:RetrieveChats(ply, text, team, death)
		
		local rp = RecipientFilter()
		for _,v in pairs( player.GetAll() ) do if v:IsAdmin() then rp:AddPlayer(v) end end
		umsg.Start("StreamChats",rp)
			umsg.String( os.date("%d/%m/%y %I:%M %p") )
			umsg.String( ply:GetName() )
			umsg.String( ply:SteamID() )
			umsg.String( text )
		umsg.End()
		table.insert( self.List, { Time = os.date("%d/%m/%y %I:%M%p"), Player = ply:GetName(), SteamID = ply:SteamID(), Message = text } )
		
	end

	hook.Add("PlayerSay", "ChatsHook", function( ply, text, team, death ) CHAT:RetrieveChats( ply, text, team, death ) end)
	
	CHAT:LoadChats()	
	
end

if CLIENT then 

	local kill_data = {}

	usermessage.Hook("StreamKills",function(u)
		local data = {}
		data.Time = u:ReadString()
		data.Killer = u:ReadString()
		data.Victim = u:ReadString()
		
		if data.Killer == data.Victim then return end
		
		table.insert(kill_data,data)
	end)
	
		local chat_data = {}

	usermessage.Hook("StreamChats",function(u)
	
		local data2 = {}
		
		data2.Time = u:ReadString()
		data2.Player = u:ReadString()
		data2.SteamID = u:ReadString()
		data2.Message = u:ReadString()
		
		table.insert(chat_data,data2)
	end)	
	
local Buttons = {

Warn = { "Warn", function() end },
ResetMap = { "Reset Map", function() RunConsoleCommand( "utopia_resetmap" ) end }

}

function admin_menu()
	
	local ply = LocalPlayer()
	
	if ply:IsModerator() or ply:IsAdmin() or ply:IsSuperAdmin() then
	
	//main menu
	
	local DermaFrame = vgui.Create( "DFrame" )
	DermaFrame:SetSize( 500, 450 )
	DermaFrame:SetTitle( "Utopia" )
	DermaFrame:SetVisible( true )
	DermaFrame:SetDraggable( true )
	DermaFrame:ShowCloseButton( true )
	DermaFrame:Center()
	DermaFrame:MakePopup()
	
	local DermaPropertySheet = vgui.Create( "DPropertySheet", DermaFrame )
	DermaPropertySheet:SetPos( 10, 32 )
	DermaPropertySheet:SetSize( DermaFrame:GetWide() - 20, DermaFrame:GetTall() - 42 )
	
	local DermaPanel = vgui.Create( "DPanel", DermaPropertySheet )
	DermaPanel:SetPos( 5, 5 )
	DermaPanel:SetSize( DermaPropertySheet:GetWide() - 10, DermaPropertySheet:GetTall() - 10 )
	DermaPanel.Paint = function()
		surface.SetDrawColor( 150, 150, 150, 0 )
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() )
	end

	local DermaList = vgui.Create( "DPanelList", DermaPanel )
	DermaList:SetPos( 10, 10 )
	DermaList:SetSize( DermaFrame:GetWide() - 390, DermaFrame:GetTall() - 92 )
	DermaList:SetSpacing( 0 )
	DermaList:EnableHorizontal( true )
	DermaList:EnableVerticalScrollbar( true )
	
	local ComboBox = vgui.Create( "DComboBox", DermaList )
	ComboBox:SetPos( 5, 5 )
	ComboBox:SetSize( 100, DermaList:GetTall() - 10 )
	ComboBox:SetMultiple( false )
	for k, v in pairs(player.GetAll()) do
		ComboBox:AddItem(v:Nick())
	end
	
	local ButtonList = vgui.Create( "DPanelList", DermaPanel )
	ButtonList:SetPos( 130, 10 )
	ButtonList:SetSize( DermaFrame:GetWide() - 170, DermaFrame:GetTall() - 92 )
	ButtonList:SetSpacing( 1 )
	ButtonList:EnableHorizontal( false )
	ButtonList:EnableVerticalScrollbar( true )
	
	local DermaTextEntry = vgui.Create( "DTextEntry", ButtonList )
	DermaTextEntry:SetPos( 10, 10 )
	DermaTextEntry:SetSize( DermaList:GetWide() - 20, 20 )
	DermaTextEntry:SetText( "Reason for kicking" )
	ButtonList:AddItem( DermaTextEntry )
	
	Buttons.Kick = { "Kick", function() RunConsoleCommand("utopia_kick", ComboBox:GetSelectedItems()[1]:GetValue(), DermaTextEntry:GetValue() or "") end }
	
	local DermaListView = vgui.Create("DListView")
		DermaListView:SetParent(DermaPanel)
		DermaListView:SetPos(25, 50)
		DermaListView:SizeToContents()

		DermaListView:SetMultiSelect(false)
		
		local Datea = DermaListView:AddColumn("Date")
		local Dateb = DermaListView:AddColumn("Victim")
		local Datec = DermaListView:AddColumn("Killer")
	
		DermaListView.OnRowSelected = function(panel , line)
     	print("\n Date = ".. DermaListView:GetLine(line):GetValue(1) .."\n Victim = ".. DermaListView:GetLine(line):GetValue(2) .."\n Killer = ".. DermaListView:GetLine(line):GetValue(3).."\n")
		Derma_Message("Printed the information to your console.")
		end

		for _,Data in pairs( kill_data ) do
			DermaListView:AddLine(Data.Time, Data.Victim, Data.Killer)
		end
	
		DermaListView:SortByColumn( 1, true )

		-----------
		
		local DermaListView2 = vgui.Create("DListView")
		DermaListView2:SetParent(DermaPanel)
		DermaListView2:SetPos(25, 50)
		DermaListView2:SizeToContents()

		DermaListView2:SetMultiSelect(false)
		
		local Dateac = DermaListView2:AddColumn("Date")
		local Datebc = DermaListView2:AddColumn("Player")
		local Datecc = DermaListView2:AddColumn("Message")

		Dateac:SetMinWidth( 100 )
		Dateac:SetMaxWidth( 100 )

		for _,Data2 in pairs( chat_data ) do
		
		DermaListView2:AddLine(Data2.Time, Data2.Player, Data2.Message)
			
		DermaListView2.OnRowSelected = function(panel , line)
     	print("\n Date = ".. Data2.Time .."\n Player = ".. Data2.Player .."\n Message = ".. Data2.Message .."\n SteamID = ".. Data2.SteamID .."\n")
		Derma_Message("Printed the information to your console.")
		end
		end
		
		DermaListView2:SortByColumn( 1 )
	
	local DermaTextEntry2 = vgui.Create( "DTextEntry", ButtonList )
	DermaTextEntry2:SetPos( 10, 10 )
	DermaTextEntry2:SetSize( DermaList:GetWide() - 20, 20 )
	DermaTextEntry2:SetText( "Time for banning: Eg. 1 (for 1 hour)" )
	ButtonList:AddItem( DermaTextEntry2 )
	
	Buttons.Ban = { "Ban", function() RunConsoleCommand("utopia_ban", ComboBox:GetSelectedItems()[1]:GetValue(), DermaTextEntry2:GetValue() or "", 0 ) end }
	
	//Unban
	
	
	local DermaPanel2 = vgui.Create( "DPanel", DermaPropertySheet )
	DermaPanel2:SetPos( 5, 5 )
	DermaPanel2:SetSize( DermaPropertySheet:GetWide() - 10, DermaPropertySheet:GetTall() - 10 )
	DermaPanel2.Paint = function()
		surface.SetDrawColor( 150, 150, 150, 0 )
		surface.DrawRect( 0, 0, DermaPanel2:GetWide(), DermaPanel2:GetTall() )
	end
	local DermaList2 = vgui.Create( "DPanelList", DermaPanel2 )
	DermaList2:SetPos( 10, 10 )
	DermaList2:SetSize( DermaFrame:GetWide() - 390, DermaFrame:GetTall() - 92 )
	DermaList2:SetSpacing( 0 )
	DermaList2:EnableHorizontal( true )
	DermaList2:EnableVerticalScrollbar( true )
	
	local ComboBox2 = vgui.Create( "DComboBox", DermaList2 )
	ComboBox2:SetPos( 5, 5 )
	ComboBox2:SetSize( 100, DermaList:GetTall() - 10 )
	ComboBox2:SetMultiple( false )
	for k, v in pairs(player.GetAll()) do
		ComboBox2:AddItem(v:Nick())
	end
	
	local ButtonList2 = vgui.Create( "DPanelList", DermaPanel2 )
	ButtonList2:SetPos( 130, 10 )
	ButtonList2:SetSize( DermaFrame:GetWide() - 170, DermaFrame:GetTall() - 92 )
	ButtonList2:SetSpacing( 1 )
	ButtonList2:EnableHorizontal( false )
	ButtonList2:EnableVerticalScrollbar( true )

	//DIS BE HAR FOR A REASON
	for k,v in pairs( Buttons ) do
		local Button = vgui.Create( "DButton", ButtonList )
		Button:SetPos( 10, 0 )
		Button:SetSize( DermaList:GetWide() - 20, 20 )
		Button:SetText( v[1] )
		Button.DoClick = 
			v[2]
		ButtonList:AddItem( Button )
	end

	
	DermaPropertySheet:AddSheet( "Banz hammer", DermaPanel, "gui/silkicons/check_off", false, false, nil )
	DermaPropertySheet:AddSheet( "Kill Log", DermaListView, "gui/silkicons/shield", false, false, nil )
	DermaPropertySheet:AddSheet( "Chat Log", DermaListView2, "gui/silkicons/user", false, false, nil )
	DermaPropertySheet:AddSheet( "Unbanz shovel", DermaPanel2, "gui/silkicons/check_on", false, false, nil )

	else
		ply:ChatPrint( "You are not allowed to access this menu!" )
	end
	end
end

usermessage.Hook( "admin_menu", admin_menu )